package com.etc.controller;

import com.etc.entity.SysUser;
import com.etc.model.User;
import com.etc.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

/**
 * 用户控制器
 */
@Controller
@RequestMapping("adminUser")
public class UserAdminController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录用户页面
     * @return
     */
    @RequestMapping(value = "adminLogin" )
    public String loginadmin (){
        return "forward:/userLogin.jsp";
    }
    /**
     * 注册用户页面
     * @return
     */
    @RequestMapping(value = "register" )
    public String register (){
        return "userAdmin/register";
    }

    /**
     * 用户首页
     * @return
     */
    @RequestMapping(value = "adminIndex" )
    public String adminIndex (){
        return "userAdmin/index";
    }


    /**
     * 用户登录
     * @param user
     * @param request
     * @return
     */
    @RequestMapping(value="/admincookie" , method= RequestMethod.POST)
    public String login(User user, HttpServletRequest request){
        try {
            SysUser list = sysUserService.findSysUser(user.getUsername(),user.getPassword());
            HttpSession session3 = request.getSession();
            session3.setAttribute("user",list);
            return "forward:/adminUser/adminIndex";
        }catch (Exception e){
            request.setAttribute("message",e.getMessage());
            return "forward:/adminUser/adminLogin";
        }
    }


    /**
     * 跳转
     * @return
     */
    @RequestMapping(value = "loginSuccess" )
    public String loginSuccess (){
        return "userAdmin/LoginSuccess";
    }



    /**
     * 注册用户
     * @return
     */
    @RequestMapping(value = "add" , method= RequestMethod.POST)
    public String add (SysUser sysUser){
        Integer restal = sysUserService.add(sysUser);
        if(restal == 1){
            return "forward:/adminUser/loginSuccess";
        }else {
            return "forward:/adminUser/register";
        }
    }


    /**
     * 用户信息
     * @return
     */
    @RequestMapping(value = "userinfo" )
    public String userInfo(HttpServletRequest request,HttpSession session,String id){
         SysUser sysUser = (SysUser) session.getAttribute("user");
         request.setAttribute("user",sysUserService.findById(String.valueOf(sysUser.getId())));
        return "userAdmin/userInfo";
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping("loginOut")
    public String loginOut(HttpServletRequest request){

        return "forward:/adminUser/adminLogin";
    }

    /**
     * 更改用户信息
     * @return
     */
    @RequestMapping(value = "userAdminUpdate", method= RequestMethod.POST)
    public void userAdminUpdate(PrintWriter out,SysUser sysUser){
       Integer restle =  sysUserService.updateUser(sysUser);
       if (restle == 1){
           out.print(1);
       }else{
           out.print(2);
       }
    }
}
